package edu.caece.langprocessor.syntax;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import edu.caece.langprocessor.lexical.tokens.SimpleLexicalToken;
import edu.caece.langprocessor.lexical.tokens.TokenType;

public class ProductionFactory {

	private String prodClass;
	private String leftSideItem;
	private List<String> rightSideItems;
	private final Set<String> lexicalVocabulary;
	
	public ProductionFactory(String prodClass, String leftSideItem, List<String> rightSideItems, Set<String> lexicalVocabulary) {
		this.prodClass = prodClass;
		this.leftSideItem = leftSideItem;
		this.rightSideItems = rightSideItems;
		this.lexicalVocabulary = lexicalVocabulary;
	}

	public Production newInstance() {

		List<GrammarItem> rightSideItems = new ArrayList<GrammarItem>();
		Production p = null;
		
		//
		for (String item : this.rightSideItems) {
			GrammarItem gi;
			if (lexicalVocabulary.contains(item)){
				gi = new Terminal(new SimpleLexicalToken(TokenType.StringToTokenType(item), ""));
			}else{
				gi = new NonTerminal(item);
			}
			rightSideItems.add(gi);
				
		}
		p = new Production(new String(prodClass), new NonTerminal(this.leftSideItem), rightSideItems);

		return p;
	}
}
